package com.group.zsxm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.fins.gt.model.FilterInfo;
import com.fins.gt.model.SortInfo;
import com.group.core.common.LimitPage;
import com.group.zsxm.service.common.BaseService;

@Service
public class DwsbqueryService extends BaseService{
	
	/**
	 * 获取单位相对某一季度的类型
	 * @param jbdm  选择的季度上报
	 * @param dwid  
	 * @return
	 */
	public Integer getDwsbLx(String jbdm,String dwid){
		int rv = 0;
		String sql = " select count(*) from dwsb where pdm='"+jbdm+"' and dm=(select dm from dwsb_mx where dwid='"+dwid+"' and substring(dm,1,4)='"+jbdm+"') ";
		if(this.getSimpleJdbcTemplate().queryForInt(sql) > 0){
			sql = " select value from dwsb where pdm='"+jbdm+"' and dm=(select dm from dwsb_mx where dwid='"+dwid+"' and substring(dm,1,4)='"+jbdm+"') ";
			rv = this.getSimpleJdbcTemplate().queryForInt(sql);
		}
		return rv;
	}
	/**
	 * 
	 * @param limit
	 * @param parMap
	 * @param sortInfo
	 * @param filterInfos
	 * @param dm  DWSB 中代码： 季报名称
	 * @return
	 */
	public Object getDwsbList(LimitPage limit, Map<String, String> parMap, SortInfo sortInfo, List<FilterInfo> filterInfos,String dwid){
		String jbdm = parMap.get("jbdm") == null ? "" :  parMap.get("jbdm");
		String jbdmlx = parMap.get("jbdmlx") == null ? "" :  parMap.get("jbdmlx");
		Map dwsb = new HashMap();
		String sql = "";
		if(!jbdm.equals("")){
			sql = " select count(*) from dwsb where pdm='"+jbdm+"' and dm=(select dm from dwsb_mx where dwid='"+dwid+"' and substring(dm,1,4)='"+jbdm+"') ";
			if(this.getSimpleJdbcTemplate().queryForInt(sql) > 0){
				sql = " select value,dm from dwsb where pdm='"+jbdm+"' and dm=(select dm from dwsb_mx where dwid='"+dwid+"' and substring(dm,1,4)='"+jbdm+"') ";
				dwsb = this.getSimpleJdbcTemplate().queryForMap(sql);
				 //001(1): 在孵企业情况   002(2)：研发机构情况   003(3): 科技园西区院校情况
				if(String.valueOf(dwsb.get("value")).equals("1")){
					sql = "select * from dwsb1_info_v where dwid='"+dwid+"' and sbdm='"+String.valueOf(dwsb.get("dm"))+"'";
				}else if(String.valueOf(dwsb.get("value")).equals("2")){
					sql = " select * from dwsb2_info_v where dwid='"+dwid+"' and sbdm='"+String.valueOf(dwsb.get("dm"))+"'";
				}else if(String.valueOf(dwsb.get("value")).equals("3")){
					sql = " select * from dwsb3_info_v where dwid='"+dwid+"' and sbdm='"+String.valueOf(dwsb.get("dm"))+"'";
				}else{
					return null;
				}
			}else{
				return null;
			}
		}else {
			
			if(jbdmlx.equals("1")){
				sql = "select * from dwsb1_info_v where dwid='"+dwid+"' ";
			}else if(jbdmlx.equals("2")){
				sql = " select * from dwsb2_info_v where dwid='"+dwid+"' ";
			}else if(jbdmlx.equals("3")){
				sql = " select * from dwsb3_info_v where dwid='"+dwid+"' ";
			}else{
				return null;
			}
		}
		
		String sortFieldName = "";
		if(sortInfo != null ){
			sortFieldName = sortInfo.getFieldName();
		}
		String sortCond = getGridSortString(sortInfo);
		String filterCond = getGridFilterString(filterInfos);
		if (filterCond != null) {
			sql += filterCond;
		}
		if (sortCond != null ) {
			if(sortFieldName.equals("SBDM_MC")){
				sql += " order by SBDM " ;
			}else{
				sql += " order by " + sortCond.split("_")[0];
			}
		} else {
			sql += " order by sbdm ";
		}
		if (limit != null) {
			return this.queryForListWithPage(sql, limit);
		} else {
			return this.simpleJdbcTemplate.queryForList(sql);
		}
	}
	

}
